package net.my.msn.service.impl;

import net.my.msn.dao.MessageDAO;
import net.my.msn.dao.UserDAO;
import net.my.msn.dao.VoiceDAO;
import net.my.msn.domain.Voice;
import net.my.msn.service.VoiceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class VoiceServiceImpl implements VoiceService {
	
	private VoiceDAO voiceDAO;
	private MessageDAO messageDAO;
	private UserDAO userDAO;
	
	@Autowired
	public void setVoiceDAO(VoiceDAO voiceDAO) {
		this.voiceDAO = voiceDAO;
	}
	@Autowired
	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}
	@Autowired
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Transactional
	public void addVoice(Voice voice, Long m_id, Long u_id) {
		if (voiceDAO.isUserVotedForThisMessage(m_id, u_id) == 0){
		    voice.setMessage(messageDAO.read(m_id));
		    voice.setUser(userDAO.read(u_id));
			voiceDAO.create(voice);
		}
	}
	
	@Transactional
	public Integer findVoiceByMessageId(Long m_id) {
		return voiceDAO.findVoiceByMessageId(m_id);
	}

}
